<?php
	// Defaults 
	$InvStr = '';
	$MtcStr = '';
	$srcQr = '';
	$gnrStr = 'INSERT INTO ex_bltmplist (mtcInvoice, mtcCliNombre, mtcCliDomicilio, mtcCliCiudad, mtcBnfNombre, mtcBnfDireccion, mtcBnfCiudad) VALUES ';
	$rsStr = '';
	$CNT = 0;
	if(isset($_POST['POOL'])) {
	$ORPOOL = strlen($_POST['POOL']);
	$ORPOOL = substr($_POST['POOL'],0,$ORPOOL-1);
	###$CORR = $_POST['CORR']; Filtro de destinos
		$srcQr = "FraID IN ({$ORPOOL})";
	}
	if(isset($_POST['rbl_DateStart'])){

		$srcQr = "FraFechaIngreso BETWEEN '{$_POST['rbl_DateStart']} 00:00:00' AND '{$_POST['rbl_DateEnd']} 23:59:59' AND FraStatus = 2";
	}
	if($srcQr!=''){
	$GET = $GLOBALS['MYT']->db_array("SELECT FraID, CliNumero, CliNombre, CliDomicilio, CliCiudad, BnfNumero, BnfNombre, BnfDireccion, BnfCiudad FROM Facturas IGNORE INDEX (IdxCli)
							INNER JOIN clientes ON CliNumero = FraCliNumero
							INNER JOIN beneficiarios ON BnfNumero = FraBnfNumero
							WHERE FraID NOT IN (SELECT blmtcINVOICE FROM ex_matches) AND ".$srcQr);
	}			
	$DBVRS = $GLOBALS['MYT']->db_array("SELECT REF, sysTime FROM ext_ofac_version ORDER BY REF DESC LIMIT 0,1");
	$CGET = $GET[0]; 
	if ($CGET>0) {
		for ($i = 1; $i <=$CGET; $i++) {
			$gnrStr .= "({$GET[$i]['FraID']},'{$GET[$i]['CliNombre']}','{$GET[$i]['CliDomicilio']}','{$GET[$i]['CliCiudad']}','{$GET[$i]['BnfNombre']}','{$GET[$i]['BnfDireccion']}','{$GET[$i]['BnfCiudad']}'),";
		}
		// SEC:_CHK->OFAC
	$GLOBALS['MYT']->db_query(substr($gnrStr,0,strlen($gnrStr)-1));
	CONN('DBP');
	$GLOBALS['MYT']->db_query("CALL blklist_match();");
	CONN('MYT');
	$GTR = $GLOBALS['MYT']->db_array("SELECT STR_MATCHED, STR_STORED, EXTERNAL_ID, blmtcTime, blmtcINVOICE, blmtcFlag, blmtcUser, blmtcObservations, FraCliNombre, FraBnfNombre, blmtcPrcCliSDN, blmtcPrcBnfSDN, blmtcPrcCliALT, FraBnfNombre, blmtcPrcBnfALT, blmtcPrcCliADD, blmtcPrcBnfADD, blmtcPos
									FROM ex_matches
										INNER JOIN facturas ON FraID = blmtcINVOICE
										INNER JOIN facturasextjoins ON FraID = FraExtID
									WHERE blmtcTime BETWEEN '{$_POST['rbl_DateStart']} 00:00:00' AND '{$_POST['rbl_DateEnd']} 23:59:59'");
	
	$CNT = $GTR[0];
	for($i=1;$i<=$CNT;$i++) {
		$rsStr .= '[\''.$GTR[$i]['EXTERNAL_ID'].'\',\''.$GTR[$i]['STR_MATCHED'].'\',\''.$GTR[$i]['STR_STORED'].'\',\'\',\''.$GTR[$i]['blmtcINVOICE'].'\',\''.$GTR[$i]['FraCliNombre'].'\',\''.$GTR[$i]['blmtcPrcCliSDN'].'\',\''.$GTR[$i]['blmtcPrcCliALT'].'\',\''.$GTR[$i]['blmtcPrcCliADD'].'\',\''.$GTR[$i]['FraBnfNombre'].'\',\''.$GTR[$i]['blmtcPrcBnfSDN'].'\',\''.$GTR[$i]['blmtcPrcBnfALT'].'\',\''.$GTR[$i]['blmtcPrcBnfADD'].'\'],';
	}
	} else {
			$Msg_ERROR="Necess�rio seleccionar remessas.";
	} 
	if (!isset($Event_Ct)) {$Event_Ct = 0;}
		#include($GLOBALS['PAD'].'Modules/FTK9D4D0/Lib/a05_elem_order');
		
	?>
<div id="rsResults"></div>
<script charset="utf-8" type="text/javascript">
disableSelection(document.getElementById("rsResults")) 

Ext.onReady(function(){

    Ext.QuickTips.init();

    var myData = [<?php echo substr($rsStr,0,strlen($rsStr)-1);?>];

    var ds = new Ext.data.Store({
        reader: new Ext.data.ArrayReader({}, [
		   {name: 'VID', type: 'string'},
		   {name: 'V1', type: 'string'},
		   {name: 'V2', type: 'string'},
		   {name: 'V3', type: 'string'},
		   {name: 'Invoice', type: 'string'},
		   {name: 'CName', type: 'string'},
		   {name: 'C1Percent', type: 'string'},
		   {name: 'C2Percent', type: 'string'},
		   {name: 'C3Percent', type: 'string'},
		   {name: 'BName', type: 'string'},
		   {name: 'B1Percent', type: 'string'},
		   {name: 'B2Percent', type: 'string'},
		   {name: 'B3Percent', type: 'string'}
        ])
    });
    ds.loadData(myData);
    var colModel = new Ext.grid.ColumnModel([
        {header: "Registo", width: 80, sortable: true, locked:true, dataIndex: 'VID'},
        {header: "Entidade", width: 150, sortable: true, locked:true, dataIndex: 'V1'},
        {header: "Nomes Alternativos", width: 150, sortable: true, locked:true, dataIndex: 'V2'},
        {header: "Morada", width: 150, sortable: true, dataIndex: 'V3'},
        {header: "Remessa", width: 80, sortable: true, dataIndex: 'Invoice'},
        {header: "Cliente", width: 120, sortable: true, dataIndex: 'CName'},
        {header: "% Nome", width: 60, sortable: true, dataIndex: 'C1Percent'},
        {header: "% Alter", width: 60, sortable: true, dataIndex: 'C2Percent'},
        {header: "% Morada", width: 60, sortable: true, dataIndex: 'C3Percent'},
        {header: "Beneficiario", width: 120, sortable: true, dataIndex: 'BName'},
        {header: "% Nome", width: 60, sortable: true, dataIndex: 'B1Percent'},
        {header: "% Alter", width: 60, sortable: true, dataIndex: 'B2Percent'},
        {header: "% Morada", width: 60, sortable: true, dataIndex: 'B3Percent'}
    ]);
	Bars = new Ext.Toolbar({ 
				
					store: this.store, 
					items : [{
						text: "Registos Verificados: <?php echo $CGET;?>"
					},{
						text: "Correspondencias registadas: <?php echo $CNT;?>"
					},{
						text: "Bloquear Correspondencias"
					},{
						text: "Adicionar a excepcoes"
					}]
				});

   var selmode = new Ext.grid.RowSelectionModel({});
   var Console = new Ext.grid.GridPanel({
        title: 'OFAC Database, version: <?php echo $DBVRS[1]['REF'];?>, <?php echo $DBVRS[1]['sysTime'];?>',
		ds: ds,
		cm: colModel,
		sm: selmode,
	    height: 500,
	    border: true,
		bbar: Bars,
        renderTo: 'rsResults'
    });
		
});
</script>